package com.nuoniu.sibanyun.controller.finance;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.finance.VReceivePaymentMain;
import com.nuoniu.sibanyun.service.finance.IVReceivePaymentMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 收付款单主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/finance/payment")
@Api(tags = "收付款单管理")
public class VPaymentMainController extends BaseController {

    @Autowired
    private IVReceivePaymentMainService paymentMainService;

    @ApiOperation("获取编码")
    @GetMapping("getCode")
    public HttpResult getCode(String sourceCode) {
        return HttpResult.ok(paymentMainService.getCode(sourceCode));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VReceivePaymentMain paymentMain) {
        if(paymentMain == null || paymentMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        paymentMainService.saveReceivable(paymentMain);
        return HttpResult.ok();
    }

    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        paymentMainService.submit(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("单据审核")
    @GetMapping("examine")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        paymentMainService.examine(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        paymentMainService.deleteById(id);
        return HttpResult.ok();
    }
}
